package com.czl.module_service.fragment.inventory

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.adapter.ViewPagerFmAdapter
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.base.route.RouteCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.databinding.FragmentInventoryAssetBinding
import com.czl.module_service.viewmodel.inventory.InventoryViewModel
import me.yokeyword.fragmentation.SupportFragment

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY)
class InventoryFragment : BaseFragment<FragmentInventoryAssetBinding, InventoryViewModel>() {

    var count0 : Int = 0
    var count1 : Int = 0
    var count2 : Int = 0
    var count3 : Int = 0

    var fragmentList = arrayListOf<SupportFragment>()

    override fun initContentView(): Int {
        return R.layout.fragment_inventory_asset
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        viewModel.tvTitle.set("资产盘点")
        viewModel.tabData.set("待盘, 正常, 盘盈, 盘亏")
        binding.viewPager2.apply {
            adapter = ViewPagerFmAdapter(childFragmentManager, lifecycle, fragments())
        }
    }

    private fun fragments(): List<SupportFragment> {
        val execId = arguments?.getString("execId")
        val taskId = arguments?.getLong("taskId")
        val status = arguments?.getInt("status")
        viewModel.execId = execId
        viewModel.taskId = taskId
        viewModel.taskName.set(arguments?.getString("taskName"))
        viewModel.locationDesc.set(arguments?.getString("locationDesc"))
        val list = arrayListOf(
            RouteCenter.navigate(
                AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY_CHILD,
                Bundle().apply {
                    putString("execId", execId)
                    taskId?.let { putLong("taskId", it) }
                    putInt("status", status!!)
                    putInt("tabIndex", 0)
                }) as SupportFragment,

            RouteCenter.navigate(
                AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY_CHILD,
                Bundle().apply {
                    putString("execId", execId)
                    taskId?.let { putLong("taskId", it) }
                    putInt("status", status!!)
                    putInt("tabIndex", 1)
                }) as SupportFragment,

            RouteCenter.navigate(
                AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY_CHILD,
                Bundle().apply {
                    putString("execId", execId)
                    taskId?.let { putLong("taskId", it) }
                    putInt("status", status!!)
                    putInt("tabIndex", 2)
                }) as SupportFragment,
            RouteCenter.navigate(
                AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY_CHILD,
                Bundle().apply {
                    putString("execId", execId)
                    taskId?.let { putLong("taskId", it) }
                    putInt("status", status!!)
                    putInt("tabIndex", 3)
                }) as SupportFragment )
        fragmentList.addAll(list)
        return list
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.tabChangeLiveEvent.observe(this) {
            binding.viewPager2.setCurrentItem(it, false)
        }
        viewModel.uc.pageChangeLiveEvent.observe(this) {
            binding.tabLayout.currentTab = it
            reload()
        }
        viewModel.uc.loadErrorEvent.observe(this) {
            showErrorStatePage()
        }
        viewModel.uc.loadCompleteEvent.observe(this) {
            showSuccessStatePage()
            count0 = it.`0`
            count1 = it.`1`
            count2 = it.`2`
            count3 = it.`3`
            viewModel.tabData.set("待盘($count0), 正常($count1), 盘盈($count2), 盘亏($count3)")
        }
        LiveBusCenter.observeInventoryResultReturnEvent(this, {
            // 盘点结果返回(tab: 0-待盘，1-正常，2-盘盈, 3-盘亏, mark: 0-正常，1-盘盈，2-盘亏, 3-删除, 4-代盘)
            /*if (it.tab == 0 && it.mark == 0) {
                count0 -= 1
                count1 += 1
            }
            if (it.tab == 0 && it.mark == 2) {
                count0 -= 1
                count3 += 1
            }
            if (it.tab == 1 && it.mark == 2) {
                count1 -= 1
                count3 += 1
            }
            if (it.tab == 2 && it.mark == 3) {
                count2 -= 1
            }
            if (it.tab == 3 && it.mark == 0) {
                count3 -= 1
                count1 += 1
            }
            viewModel.tabData.set("待盘($count0), 正常($count1), 盘盈($count2), 盘亏($count3)")*/

            fragmentList.forEach { data ->
                if (data is InventoryChildFragment) {
                    data.loaded = false
                }
            }
            reload()
        })
        // 盘盈
        LiveBusCenter.observeRegisterResultReturnEvent(this, {
            count2 += 1
            viewModel.tabData.set("待盘($count0), 正常($count1), 盘盈($count2), 盘亏($count3)")
            LiveBusCenter.postInventoryHomeReturnEvent(1)
        })
    }

    override fun reload() {
        val execId = arguments?.getString("execId")
        val taskId = arguments?.getLong("taskId")
        viewModel.wechatGetTaskFacilityInfoCountByTaskId(execId, taskId, "")
    }
}